package com.cqupt.service.serviceImp;

import com.cqupt.dao.PackageMapper;

import com.cqupt.paging.Paging;
import com.cqupt.pojo.Complaint;
import com.cqupt.pojo.Package;
import com.cqupt.pojo.PackageExample;
import com.cqupt.service.PackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 蒋俊贤
 * @version 1.0
 */
@Service
public class PackageServiceImp implements PackageService {
    @Autowired
    private  PackageMapper packageMapper;
    //查询套餐
    @Override
    public List selectPackage(Integer number){
        Paging paging = new Paging();
        PackageExample packageExample = new PackageExample();
        packageExample.createCriteria().andIdIsNotNull();
        List<Package> packages = packageMapper.selectByExample(packageExample);
        if(packages.size() <= 8){
            paging.setList(packages);
            paging.setNumber(packages.size());
        }
        List<Package> packageLimit = new ArrayList<>();
        if(packages.size() <= 8*number){
            for (int i = 8 * (number - 1); i < packages.size(); i++) {
                packageLimit.add(packages.get(i));
            }
            paging.setList(packageLimit);
            paging.setNumber(packages.size());
           return paging.getList();
        }
        for (int i = 8 * (number - 1); i < 8 * number; i++) {
            packageLimit.add(packages.get(i));
        }
        paging.setList(packageLimit);
        paging.setNumber(packages.size());
        return paging.getList();

    }
    //新增套餐
    @Override
    public int insertPackage(Package pac){
        return packageMapper.insertSelective(pac);
    }

    @Override
    //修改
    public int updatePackage(Package pac) {
        System.out.println(pac.getPicpath());
        Package tbpacage = null;
        PackageExample packageExample = new PackageExample();
       if(pac.getId() != null){
           packageExample.createCriteria().andIdEqualTo(pac.getId());
       }
        List<Package> packages = packageMapper.selectByExample(packageExample);
       if(packages.size() > 0){
           tbpacage = packages.get(0);
       }
       if(pac.getName() != null){
           tbpacage.setName(pac.getName());
       }
       if(pac.getPrice() != null){
           tbpacage.setPrice(pac.getPrice());
       }
       if(pac.getType() != null){
           tbpacage.setType(pac.getType());
       }
       if(pac.getFlow() != null){
           tbpacage.setFlow(pac.getFlow());
       }
        if(pac.getScore() != null){
            tbpacage.setScore(pac.getScore());
        }
        if(pac.getState() != null){
            tbpacage.setState(pac.getState());
        }
        if(pac.getDescription() != null){
            tbpacage.setDescription(pac.getDescription());
        }
        if(pac.getPicpath() != null){
            tbpacage.setPicpath(pac.getPicpath());
        }
        return packageMapper.updateByPrimaryKeySelective(tbpacage);



    }
//删除套餐
    @Override
    public int deletePackage(Integer id) {
        return  packageMapper.deleteByPrimaryKey(id);
    }
//上架套餐
    @Override
    public int onlinePackage(Integer id) {
        PackageExample packageExample = new PackageExample();
        packageExample.createCriteria().andIdEqualTo(id);
        List<Package> packages = packageMapper.selectByExample(packageExample);
        Package pac = new Package();
        pac = packages.get(0);
        pac.setState(1);
        return packageMapper.updateByPrimaryKeySelective(pac);
    }
//下架套餐
    @Override
    public int offlinePackage(Integer id) {
        PackageExample packageExample = new PackageExample();
        packageExample.createCriteria().andIdEqualTo(id);
        List<Package> packages = packageMapper.selectByExample(packageExample);
        Package pac = new Package();
        pac = packages.get(0);
        pac.setState(0);
        return packageMapper.updateByPrimaryKeySelective(pac);
    }
}
